package com.erp.bus.Controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.bus.comm.Code;
import com.erp.bus.comm.DataAndView;
import com.erp.bus.comm.ResultCode;
import com.erp.bus.comm.ReturnGood;
import com.erp.bus.pojo.Goods;
import com.erp.bus.pojo.Inport;
import com.erp.bus.pojo.Outport;
import com.erp.bus.pojo.Provider;
import com.erp.bus.service.GoodsService;
import com.erp.bus.service.InportService;
import com.erp.bus.service.OutportService;
import com.erp.bus.service.ProviderService;
import com.erp.bus.vo.OutportVo;
import com.erp.sys.Util.WebUtil;
import com.erp.sys.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("outport")
public class OutportController {

    @Autowired
    private OutportService outportService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ProviderService providerService;
    @Autowired
    private InportService inportService;

    //http://localhost:8080/outport/loadAllOutport?page=1&limit=10
    @RequestMapping("loadAllOutport")
    public DataAndView loadAllOutport(OutportVo outportVo) {
        IPage<Outport> page = new Page<Outport>(outportVo.getPage(), outportVo.getLimit());

        QueryWrapper<Outport> qw = new QueryWrapper<>();
        // 如果条件不为空,就添加
        if( outportVo.getProviderid()!=null && outportVo.getProviderid()!=0){
            qw.like("providerid",outportVo.getProviderid());
        }
        if( outportVo.getGoodsid()!=null && outportVo.getGoodsid()!=0){
            qw.like("goodsid",outportVo.getGoodsid());
        }

        //把String  转化为  Date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = null;
        Date endTime = null;

        if(outportVo.getStartTime()!=null && outportVo.getStartTime()!= ""){
            try{
                startTime = sdf.parse(outportVo.getStartTime());
                System.out.println(startTime);
            }catch (Exception e){
            }
            qw.gt("outputtime",new Timestamp(startTime.getTime()));
        }
        if(outportVo.getEndTime()!=null && outportVo.getEndTime() != ""){
            try{
                endTime =  sdf.parse(outportVo.getEndTime());

                System.out.println();
            }catch (Exception e){
            }
            qw.lt("outputtime",new Timestamp(endTime.getTime()));
        }

        if (outportVo.getOperateperson() != null){
            qw.like("operateperson",outportVo.getOperateperson());
        }
        if(outportVo.getRemark()!=null){
            qw.like("remark",outportVo.getRemark());
        }

        // 开始查询
        outportService.page(page,qw);
        List<Outport> list = page.getRecords();
        List<Outport> list2 = new ArrayList<>();
        for (Outport outport : list) {
            Integer provideId = outport.getProviderid();
            Provider provider = providerService.getById(provideId);
            outport.setProvidername(provider.getProvidername());

            Integer goodsId = outport.getGoodsid();
            Goods good = goodsService.getById(goodsId);
            outport.setGoodsname(good.getGoodsname());
            outport.setSize(good.getSize());

            list2.add(outport);
        }

       return  new DataAndView(list2);

    }

    // 添加
    //http://localhost:8080/outport/addOutport
    @RequestMapping("addOutport")
    public ResultCode addOutport(ReturnGood returnGood){

        System.out.println(returnGood);
        // 获取当前进货数据
        Inport inport= inportService.getById(returnGood.getId());

        // 设置退货信息
        Outport outport =  new Outport();
        // 设置商品名称
        Goods good = goodsService.getById(inport.getGoodsid());
        outport.setGoodsname(good.getGoodsname());
        outport.setProvidername(providerService.getById(inport.getProviderid()).getProvidername());
        outport.setSize(good.getSize());
        outport.setGoodsid(inport.getGoodsid());
        outport.setNumber(returnGood.getNumber());
        HttpSession session = WebUtil.getSession();
        User user = (User) session.getAttribute("user");
        outport.setOperateperson(user.getName());
        outport.setOutputtime(new Date());
        outport.setOutportprice(inport.getInportprice());
        outport.setRemark(returnGood.getRemark());
        outport.setPaytype(inport.getPaytype());
        outport.setProviderid(inport.getProviderid());

        //  退多少货 ，进货要对应减少
        //  退货数量<进货数量
        Long num1 = returnGood.getCurrentnumber();  // Long 包装类
        long num2 = num1;     // long   基本数据类型
        int num3 = (int)num2;
        inport.setNumber(num3-returnGood.getNumber());
        if(inport.getNumber() == 0){
            inportService.removeById(inport);
        }else{
            inportService.updateById(inport);
        }


        boolean bool = outportService.save(outport);

        if(bool){
            return ResultCode.SUCCESS_ADD;
        }else{
            return ResultCode.FAIL_ADD;
        }


    }
}
